/**
* @Title: problemCheckList_Ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ResearchCheckListCtrl', ['$scope','$state','$http','$modal','CaseAuditListService','CaseCommonService',
	                                           'BaseURL','Modal','FwaTabsetService','AuditDictServices','InsuranceService',
	                                function($scope,$state,$http,$modal,caseAuditListService,CaseCommonService,
	                                		baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService){

		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
		
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas();
		
        var cellTemplate = '<a ng-show="data.problemStatus == 3" ng-click="operation(data);">审核</a>' +
            '<a ng-show="data.problemStatus == 6" ng-click="operation(data);">审核</a>';
		$scope.columns = [
  		{
			label : '事件号',
			map : 'problemNo',
			width : '10%',
			 cellTemplate:'<a  ng:bind="data.problemNo" '+
			 	'ng-click="queryByProblemCode(data)"></a>'}, 
		{
			label : '事件时间段',
			map : 'periodDate',
			width : '15%'
		}, {
			label : '事件要点',
			map : 'keyPoint',
			width : '20%'
		}, {
			label : '来源方式',
			map : 'sourceTypeDesc',
			width : '10%'
		}, {
			label : '提交日期',
			map : 'submitDate',
			width : '15%'
		}, {
			label : '关联案件号',
			map : 'otherCaseCode',
			width : '10%'
		}, {
			label : '状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : 'operation',
			width : '10%',
            cellTemplate:cellTemplate
		}];
		
		$scope.operation = function(data){
			if(data.problemStatus == 6){//调查待审核
				$state.go('researchCheck.researchConclusionCheck',{problemNo:data.problemNo,urlFlag:''});
			}
			else { //查看
				 $scope.queryByProblemCode(data);
			}
		}
		
		 $scope.queryByProblemCode = function(data){
             $state.go("problemDetailView.detail",{problemCode:data.problemNo,problemStatus:data.problemStatus});
		  }
		
		$scope.onReset = function(){
            $scope.params.problemCode=null;
            $scope.params.orgName=null;
            $scope.params.hearDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	    };

       $scope.currentPage = 1;
        var start = 0;
        $scope.limit = 20;
        $scope.params = {
                page:0,
                pageSize:20
            };

        //分页监听
        $scope.$on('pageTool.skipPage',function(e,v){
            $scope.currentPage = v;
            start = $scope.limit*(v-1);
            $scope.onSubmit(true);
        });

	    $scope.onSubmit = function(resetPage){
	    	 $scope.loading = true;
            $scope.params.page=0;
            
            if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
	    	var fparam = $scope.params;
            if($scope.params.hearDate){
                var hearDateStart = $scope.params.hearDate.start;
                var hearDateEnd = $scope.params.hearDate.end;
                fparam.hearDateStart = hearDateStart;
                fparam.hearDateEnd = hearDateEnd;
            }
            if($scope.params.sbmitDate){
                var start = $scope.params.sbmitDate.start;
                var end = $scope.params.sbmitDate.end;
                fparam.start = start;
                fparam.end = end;
            }
	    	//查询条件少一个案件编号
	    	fparam.problemStatus ="\'6\'";
	    	CaseCommonService.loadProblemTask(fparam).success(function(response){
	    		 $scope.loading = false;
	    		$scope.tableData=response.root;
	    		$scope.totalCount =response.total;
	    	});
	    	
	    }
	    
	    
	    
	    
	    
	    
        //************批量操作begin**************
        /**
         * 获取勾选的总记录数
         */
        $scope.checkRows = [];
        
        function findCheck(d) {
        	for(var i=0;i< $scope.checkRows .length;i++) {
                 var checkedData =  $scope.checkRows [i];
               if(checkedData.problemNo == d.problemNo) {
                     return i;
                 }
             }
             return -1;
         }

         $scope.$on('fwatable.checked', function(e,d){
             if(findCheck(d) == -1) {
                 $scope.checkRows.push(d);
             }
         });

         $scope.$on('fwatable.unchecked', function(e,d){
             var index = findCheck(d);
             if(index != -1) {
                 $scope.checkRows.splice(index,1);
             }
         });
    
     /**
      * 调查审核批量提交
      */
      $scope.batchSumit = function(){
    	  $scope.auditObj = {};
    	  if($scope.checkRows.length>0){
    		    var flag = false;
    		 	for(var i = 0;i< $scope.checkRows.length;i++) {
                    var checkedData = $scope.checkRows[i];
                    var caseStatus = checkedData.problemStatus;
                    if(caseStatus != '6'){
                    	flag = true;
                    }
                }
    		if(flag){
    		 		 Modal.alert("只能选择同一状态进行批量操作！");
           		     return;
    		}else{
    			   popDialogFun();
    		     }
    		 
    	  }else{
    		  Modal.alert("请勾选要批量提交的记录！");
    		  return;
    	  }
      }
     /**
      * 批量操作弹出框
      */
     function popDialogFun(){
    	var scope = $scope.$new();
 		var modalInstance = $modal.open({
 		      templateUrl: baseUrl+'views/hcpms/audit/problemCheck/problemCheckBatchSubmitDialog_view.html',
 		      backdrop:'static',
 			  size:'sm',
 			  scope:scope,
 			  controller: function($scope, $http,$modalInstance,Modal){
 				  	 //提交
 				  $scope.batchConfirm=function(){
 					  if(!$scope.auditObj.opinion || $scope.auditObj.opinion==''){
 						  		Modal.alert('请选择意见！');
 						  		return;
     					 }else if(!$scope.auditObj.opinionDesc){
     						 	Modal.alert('意见描述不能为空！');
				    	    	return;
     				     }
     				     var batchProblemCode=new Array();
     				     for(var i=0;i< $scope.checkRows.length;i++) {
     				    		var checkedData =  $scope.checkRows[i];
     				    		batchProblemCode.push(checkedData.problemNo);
     				 	 }
     				     var obj={
     				    		 problemCodes:batchProblemCode,
     				    		 opinion:$scope.auditObj.opinion,
     				    		 opinionDesc:$scope.auditObj.opinionDesc
     				     };
     				     submitSelectAllRecord(obj, $scope);
     				 }
     				 
 					//取消
 					$scope.onClose = function() {
 							$modalInstance.close();
 					}
 					
 			     }
 		      });
 			  
 		}
     
     /**
      * 开始提交批量选择的记录
      */
     function submitSelectAllRecord(parameter, $scope){
    	 $scope.loading = true;
    	 $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'auditReviewResultSurvey/researchCheckBatchSubmit.do',
				params :{
					problemCodes:parameter.problemCodes,
					opinion:parameter.opinion,
					opinionDesc:parameter.opinionDesc
				}
			}).success(function(data) {
				 $scope.loading = false;
				if(data!=null && data.state){
					Modal.alert(data.msg);
					if(data.state=='true'){
						$scope.onSubmit();
					}
					$scope.onClose();//关闭弹出框
				}
			});
     }
     //************批量操作end**************
	    
     
     
     
     
     
	    
	    function getSourceType(code){
			if(code=='1'){
				return '其他部门转交';
			}else if(code=='2'){
				return '投诉';
			}else if(code=='3'){
				return '巡查';
			}else if(code=='4'){
				return '其他';
			}
		}
	    
	    $scope.onSubmit();
	}]);
})();
